package bTree.LeetCode;

public class 加油站 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int len = gas.length;
        for (int i = 0; i < len; i++) {
            int cnt = 0;
            int sum = 0;
            for (int j = 0; j < len; j++) {
                sum += gas[(i + j) % len];
                int cost1 = cost[(i + j) % len];
                if (sum < cost1) {
                    i = i + j;
                    break;
                }
                sum -= cost1;
                cnt++;
                if (cnt == len) {
                    return i;
                }
            }
        }
        return -1;
    }
}